<?php 
	/**
	* FILE: ProcessMonthlyISR.php
	* PATH: /web_root/models/
	* NOTES:
	*	works for VAIL demo 2008-10-23
	*/
	
include('../includes/phpheader.php');
include('../includes/db_connect.php'); 

  //consumer ID
  $id = $_POST["id"];

  //casenote ID
  $isrId = $_POST["isrId"];
 
  //I&R ID
  $irId = $_POST["irId"];


  if(!$irId) {
	$irId="NULL";
  }



$userId = $_SESSION['user_id'];

$caseNoteMonth = $_POST["caseNoteMonth"];
$caseNoteYear = $_POST["caseNoteYear"];
$caseNoteCoordinator = $_POST["caseNoteCoordinator"];
$serviceType = $_POST["serviceType"];
$totalServiceContacts = $_POST["totalServiceContacts"];
$contactByPerson = $_POST["contactByPerson"];
$contactByPhone = $_POST["contactByPhone"];
$contactByMail = $_POST["contactByMail"];

if(!$contactByPerson) 		$contactByPerson=0; 		else 	$contactByPerson=1;
if(!$contactByPhone) 		$contactByPhone=0; 		else 	$contactByPhone=1;
if(!$contactByMail) 		$contactByMail=0; 		else 	$contactByMail=1;

$services = $_POST["services"];
$serviceHourTotals = $_POST["serviceHourTotals"];


$query = "select id from monthlyISR where consumer_id = ".$id." and id = ".$isrId;
if(mysql_fetch_array(mysql_query($query))) {
	$query = "UPDATE `monthlyISR` set `ir_id` = ".$irId.", `case_note_month` = '".$caseNoteMonth."', `case_note_year` = '".$caseNoteYear."',".
	" `case_note_coordinator` = '".$caseNoteCoordinator."',	`service_type_id` = '".$serviceType."' , `total_service_contacts`  = '".
	$totalServiceContacts."', `contact_by_person` ='".$contactByPerson."', `contact_by_phone` ='".$contactByPhone.
	"', `contact_by_mail` = '".$contactByMail."', `services` = '".$services."',	`service_hour_totals` = '".$serviceHourTotals."' 
	WHERE consumer_id = '".$id."';";
} else {
	$query = "INSERT INTO `monthlyISR` ( `id` , `ir_id`, `consumer_id` , `case_note_month` , `case_note_year` , `case_note_coordinator` ,
	`service_type_id` , `total_service_contacts` , `contact_by_person` , `contact_by_phone` , `contact_by_mail` , `services` ,
	`service_hour_totals`, `entered_by` )
		VALUES (NULL , ".$irId.", '".$id."', '".$caseNoteMonth."', '".$caseNoteYear."', '".$caseNoteCoordinator."', '".serviceType."', '".
		$totalServiceContacts."', '".$contactByPerson."', '".$contactByPhone."', '".$contactByMail."', '".$services."', '".
		$serviceHourTotals."', ".$userId.");";
}


if(mysql_query($query)) {
	$misrIdquery = "select id from monthlyISR where consumer_id = ".$id;
	$misrIdrow = mysql_fetch_array(mysql_query($misrIdquery));
	$misrId = $misrIdrow["id"];

	//echo "MisrId: ".$misrId;
	$categoryQuery = "select id from lst_service_category";
	$categoryResult = mysql_query($categoryQuery);
	while($categoryRow = mysql_fetch_array($categoryResult)) {
		$categoryId = $categoryRow["id"];
		$category = $_POST[$categoryId."_category"];
		$categoryHours = $_POST[$categoryId."_hours"];
		if($category) {
			//echo "Category: ".$category." Hours: ".$categoryHours."<br/>";
			
			$misr_service_query = "select id from monthlyISR_service where monthlyISR_id = '".$misrId."' and service_category_id = '".
				$categoryId."'";
			//echo "<br/>Misr_service_query: ".$misr_service_query."<br/>";
			
			$misr_result = mysql_query($misr_service_query);
			if($misr_row = mysql_fetch_array($misr_result)) {
				$misr_insert = "update monthlyISR_service set hours = '".$categoryHours."' where id = ".$misr_row["id"];
			} else {
				$misr_insert = "insert into monthlyISR_service (monthlyISR_id, service_category_id, hours) 
					VALUES 
						('".$misrId."', '".$categoryId."', '".$categoryHours."')";
			}
			if(mysql_query($misr_insert)) {
				//echo "<br/>Success<br/>".$misr_insert;
			} else {
				//echo "<br/>Error<br/>".$misr_insert;
			}
			
		}
	}

  header("Location:../admin.php?action=cieedit&id=".$id."&Status=Monthly Individual Service Record Form Submitted Successfully");
  exit();
	
	echo "Success <br/>".$query;
} else {

        die('Could not connect: ' . mysql_error());
	//header("Location:../admin.php?action=cieedit&id=".$id."&Error=Unable to process Monthly Individual Service Record Form due to system error.  Please contact system administrator.");
  exit();
	echo "User:".$userId;
	echo "Failure <br/>".$query;
}
?>
